package com.shushan.pages;


import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shushan.base.BaseActivity;
import com.shushan.exam.ExamActivity;
import com.shushan.shushanbao.BuildConfig;
import com.shushan.shushanbao.R;
import com.shushan.ui.VideoView;
import com.shushan.util.DialogUtil;
import com.shushan.util.ImageUtils;
import com.shushan.util.MediaController;
import com.shushan.util.NetWorkUtils;
import com.shushan.util.SharedPreferencesUtils;
import com.shushan.util.ToolUtils;

import java.util.HashMap;

import cn.jzvd.JZVideoPlayerStandard;

/**
 * @ClassName: SpecialTopicVideoActivity
 * @Desciption: //播放专题练习视频的页面
 * @author: zhangshihao
 * @date: 2018-04-13
 */
public class SpecialTopicVideoActivity extends BaseActivity implements JZVideoPlayerStandard.OnVideoFinishListener
    ,View.OnClickListener
{

    private static final String TAG = SpecialTopicVideoActivity.class.getSimpleName();

    private ImageView ivLeftArrow;
    private JZVideoPlayerStandard jzVideoPlayerStandard;
    private Bitmap firstFrame;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            jzVideoPlayerStandard.thumbImageView.setImageBitmap(firstFrame);
        }
    };

    private String video_title;
    private String video_url;
    private boolean isPlayResume = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        hideStatusBar(this);
        setContentView(R.layout.activity_special_topic_video);

        Intent intent = getIntent();
        video_title = intent.getStringExtra("video_title");
        video_url = intent.getStringExtra("video_url");

        initView();
    }

    private void initView() {
        jzVideoPlayerStandard = findViewById(R.id.jz_video_player);
        jzVideoPlayerStandard.setOnVideoFinishListener(this);
        setFirstFrameDrawable(video_url);
        jzVideoPlayerStandard.setUp(video_url,JZVideoPlayerStandard.SCREEN_WINDOW_NORMAL,video_title);
        ivLeftArrow = findViewById(R.id.iv_left_arrow);
        ivLeftArrow.setOnClickListener(this);
    }


    @Override
    protected void onResume() {
        super.onResume();

        if(isPlayResume){
            jzVideoPlayerStandard.goOnPlayOnResume();

            isPlayResume = false;
        }

    }

    @Override
    protected void onPause() {
        super.onPause();

        jzVideoPlayerStandard.goOnPlayOnPause();

        isPlayResume = true;
    }

    private void setFirstFrameDrawable(final String url){
        new Thread(new Runnable() {
            @Override
            public void run() {
                MediaMetadataRetriever mmr = new MediaMetadataRetriever();
                Log.d(TAG,"video_url = "+url);
                String loadUrl;
                if(ImageUtils.isAbsoluteUrl(url)){
                    loadUrl = url;
                }else{
                    loadUrl = BuildConfig.imgBaseUrl+url;
                }
                Log.d(TAG,"loadUrl = "+loadUrl);
                try {
                    mmr.setDataSource(loadUrl,new HashMap<String, String>());
                    firstFrame = mmr.getFrameAtTime();
                    mHandler.sendEmptyMessage(0);
                }catch (IllegalArgumentException ex){
                    ex.printStackTrace();
                }catch (RuntimeException ex){
                    ex.printStackTrace();
                } finally {
                    try {
                        mmr.release();
                    } catch (RuntimeException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        }).start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        jzVideoPlayerStandard.release();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_left_arrow://回退键
                onBackAction();
                break;
        }
    }

    @Override
    public void onVideoFinish() {
        onBackAction();
    }

}
